﻿// ==UserScript==
// @name       Auto Get Code
// @author  Sunny
// @namespace  http://userscript.sunnyrat.me/
// @version    0.9.0.3
// @description 上海电力学院验证码自动识别工具 
// @description v0.9.0更新：
// @description 1.应对学校更新验证码进行了同步更新
// @run-at document-end
// @include    http://210.35.88.75:7777/schoolmanager/
// @include    http://210.35.88.75:7777/schoolmanager/index.jsp
// @include    http://210.35.88.76:7777/schoolmanager/
// @include    http://210.35.88.76:7777/schoolmanager/index.jsp
// @include    http://116.236.204.10:7777/schoolmanager/
// @include    http://116.236.204.10:7777/schoolmanager/index.jsp
// ==/UserScript==

(function(){
	var specialValidate = (location.host.indexOf("210.35.88.75") >= 0)
	var getImageObj = function(imgs , srcstr){
		for (var i = 0 ; i < allImgs.length ; i++){
			if (allImgs[i].src.indexOf(srcstr) >= 0){
				return allImgs[i] ;
			}
		}
		return null ;
	}

	var canvasObj = null ;
	var allImgs = document.getElementsByTagName("img") ;
	var codeImg = getImageObj(allImgs , "/RandomImgServlet") ;
	var form = document.forms[0] ;

	codeImg.style.display = "None" ;

	codeImg.onload = function(){
		if (codeImg && (!canvasObj)){
			canvasObj = document.createElement('canvas');
			canvasObj.setAttribute("height","15px") ;
			canvasObj.setAttribute("width" , "40px") ;
			codeImg.parentNode.insertBefore(canvasObj, codeImg);
		}
		canvasObj.onclick = function(){codeImg.src = codeImg.src ;}
		var cxt = canvasObj.getContext("2d");
		cxt.drawImage(codeImg,0,0);
		getImageData = cxt.getImageData(0,0,canvasObj.width,canvasObj.height);
		imageData = getImageData.data ;

		numDict = new Array() ;
		numDict[0] = specialValidate ? 0x3e880022 : 0x3e8808be;
		numDict[1] = specialValidate ? 0x2003ef80 : 0x2083ef80 ;
		numDict[2] = specialValidate ? 0x2184b38 : 0x2104e38 ;
		numDict[3] = specialValidate ? 0x20822a : 0x82082b6 ;
		numDict[4] = specialValidate ? 0xc52413e : 0xc134fbe ;
		numDict[5] = specialValidate ? 0x3000000a : 0x28e ;
		numDict[6] = specialValidate ? 0x3ea8000a : 0x3e88820e ;
		numDict[7] = specialValidate ? 0x8639c : 0x218c630 ;
		numDict[8] = specialValidate ? 0x3628820a : 0x3e208336 ;
		numDict[9] = specialValidate ? 0x3a904126 : 0x38000abe ;

		var getNumChar = function(numArr){
			for (var i = 0 ; i < 10 ; i++){
				if (numDict[i] === numArr){return i;}
			}
			for (var i = 0 ; i <= 9 ; i++){
				if(parseInt((numDict[i] ^ numArr).toString(2).split('').sort().join('') , 2) < 4){return i;}
			}
			return '-1' ;
		}
		
		var codeNum = '' ;
		
		var offsetLeft = 4 , picWidth = 5 ;
		for (var i = 0 ; i < 4 ; i++){
			var currentNum = undefined ;
			for (var pixX = offsetLeft ; pixX < (offsetLeft + picWidth) ; pixX++){
				for (var pixY = 4 ; pixY <= 13 ; pixY += ((pixY-4)? 2 : 1)){
					var colorG = imageData[(pixY * 40 + pixX) * 4 + 1] ;
					var colorB = imageData[(pixY * 40 + pixX) * 4 + 2] ;
					currentNum <<= 1 ;
					if ((colorG + colorB) / 2 <= 120){currentNum ^= 1 ;}
				}
			}
			offsetLeft += (specialValidate ? 10 : 9) ;
			codeNum += getNumChar(currentNum) ;
		}
		
		if (codeNum.length != 4){
			codeImg.src = "/schoolmanager/RandomImgServlet?timeStamp=" + Math.random()  ;
		}else{
			form.validateCode.value = codeNum;
			form.validateCode.setAttribute("readOnly" , "true") ;
			form.validateCode.style.backgroundColor = "#ebebe4" ;
			form.validateCode.style.color = form.validateCode.style.borderBottomColor = "#545454";
			form.pwd.setAttribute("onkeydown" , "if (event.keyCode==13){checkForm()}") ;
			getImageObj(allImgs , "images/login.gif").setAttribute("onkeydown" , "if (event.keyCode==13){checkForm()}") ;
			form.loginName.setAttribute("tabindex" , "1") ;
			form.pwd.setAttribute("tabindex" , "2") ;
			getImageObj(allImgs , "images/login.gif").setAttribute("tabindex" , "3") ;
			form.loginName.focus() ;
		}
	}
	if(codeImg.complete && !canvasObj){codeImg.onload();}
	codeImg.src = "/schoolmanager/RandomImgServlet" ;
})();